package com.paipaideli.ui.home.adapter.model;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.airbnb.epoxy.EpoxyHolder;
import com.airbnb.epoxy.EpoxyModelWithHolder;
import com.paipaideli.R;
import com.paipaideli.common.banner.BannerView;
import com.paipaideli.common.utils.IntentUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.ui.home.adapter.bean.BannerIntentBean;
import com.paipaideli.ui.home.bean.HomeBean;
import com.paipaideli.ui.webView.WebAcyivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页banner item
 * TODO("banner ")
 */

public class BannerModel extends LinearLayout implements BannerView.BannerOnClickListener {

    @BindView(R.id.banner)
    BannerView bannerview;
    private List<String> banner = new ArrayList<>();
    private List<String> title = new ArrayList<>();
    private List<String> url = new ArrayList<>();
    private List<HomeBean.Data.BannerRes> banners = new ArrayList<>();
    private Context context;

    public BannerModel(Context context) {
        super(context);
        banner.add("");
        this.context = context;
        init();
    }

    public BannerModel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        banner.add("");
        this.context = context;
        init();
    }

    public BannerModel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        banner.add("");
        this.context = context;
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressWarnings("unused")
    public BannerModel(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        banner.add("");
        this.context = context;
        init();
    }

    public void init() {
        View inflate = View.inflate(context, R.layout.home_banner, this);
        ButterKnife.bind(inflate);
    }


    public void addBanner(String bannerItem,String title,String url) {
        this.banner.add(bannerItem);
        this.title.add(title);
        this.url.add(url);
        //2、通过Resources获取屏幕的宽高设置banner高度
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int width = dm.widthPixels;
        ViewGroup.LayoutParams para;
        para = bannerview.getLayoutParams();
        para.width = width;
        para.height = 300*width/750;
        bannerview.setLayoutParams(para);
        bannerview.setList(banner);
        bannerview.setOnClickListener(this);
    }

    public void setBanners(List<HomeBean.Data.BannerRes> banners) {
        this.banners = banners;
    }

    public void clear() {
        banner.clear();
        banners.clear();
        title.clear();
        url.clear();
    }

    @Override
    public void onClickListener(int pos) {
        try {
            if (banners != null && pos <= banners.size() - 1) {

                if(!TextUtils.isEmpty(url.get(pos))){
                    WebAcyivity.open(context,url.get(pos),title.get(pos));
                }


//                HomeBean.Data.BannerRes item = banners.get(pos);
//                BannerIntentBean bannerIntentBean = new BannerIntentBean(item.imagePath, item.externalLinks, item.id, item.position, item.title, item.content);
//                IntentUtil.ClickIntents(context, bannerIntentBean, "00004", "");
            }
        } catch (Throwable e) {
            Log.d("Banner", "banner item clicked error.");
            Log.e("BannerModel", "error", e);
        }
    }
}
